
//
//  HouseTakeViewController.m
//  sellHouse
//
//  Created by 李 佳伟 on 2018/11/28.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "HouseTakeViewController.h"
#import "JTCSM.h"
#import "HouseTakeTableViewCell.h"
#import "HouseTakeModel.h"
#import "MJRefresh.h"

static CGFloat headerViewHeight = 35;
static CGFloat headerLabelHeight = 20;
@interface HouseTakeViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger currentPage;
}
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *dynamicList;

@end

@implementation HouseTakeViewController

#pragma mark 页面返回
- (void)gotoback{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:RGBACOLOR(241, 241, 241, 1)];
    currentPage = 1;
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem= [leftbtn setBarButtonItemsImageName:@"erweimafanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotoback)];
    [df isRemoveTarbar:YES];
    [df lineViewWithHeightofTop:Height_NavBar+10 superView:self.view lineHeight:10 space:0];
    [df lineViewWithHeightofTop:Height_NavBar+20 superView:self.view lineHeight:10 space:0].backgroundColor= [UIColor whiteColor];
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, Height_NavBar+20, KCONTROLLERWith, KSCREENHeight-(Height_NavBar+20)) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSelectionStyleNone;
    _tableview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableview];
    
    __block HouseTakeViewController *myself = self;
    // 下拉刷新
    _tableview.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [myself setRequest];
        });
    }];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    _tableview.mj_header.automaticallyChangeAlpha = YES;
    
    // 上拉刷新
    _tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            myself->currentPage++;
            [myself setMoreRequest];
        });
    }];
    // Do any additional setup after loading the view.
    [self setRequest];
    // Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dynamicList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    HouseTakeModel *model = self.dynamicList[section];
    return model.dynamicList.count;
//    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellid = [NSString stringWithFormat:@"%ld-%ld",indexPath.section,indexPath.row];
    HouseTakeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell==nil) {
        cell = [[HouseTakeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section>=self.dynamicList.count || indexPath==nil) {
        return cell;
    }
    HouseTakeModel *model = self.dynamicList[indexPath.section];
    if (indexPath.row>=model.dynamicList.count || indexPath ==nil) {
        return cell;
    }
    HouseTakeModel *cellModel = [HouseTakeModel whc_ModelWithJson:model.dynamicList[indexPath.row]];
    [cell setvalueWithModel:cellModel];
   
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
   
    UIView *headerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWidth, headerViewHeight)];
    headerview.backgroundColor = [UIColor whiteColor];
    
    UIView *lineview = [[UIView alloc]initWithFrame:CGRectMake(Kspace+(dianviewsize-1)/2, 0, 1, headerViewHeight)];
    lineview.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    [headerview addSubview:lineview];
    
    UIView *dianview = [[UIView alloc]initWithFrame:CGRectMake(Kspace, (headerLabelHeight-dianviewsize)/2, dianviewsize, dianviewsize)];
    dianview.backgroundColor = RGBACOLOR(15, 155, 232, 1);
    [df layerWithView:dianview radius:dianviewsize/2 width:0 color:nil];
    [headerview addSubview:dianview];
    
    if (section>=self.dynamicList.count) {
        return headerview;
    }
    HouseTakeModel *model = self.dynamicList[section];
    
    UILabel *titleLabel = [UILabel initWithFrame:CGRectMake(KVIEW_X(dianview)+10, 0, KSCREENWidth-10-dianviewsize-Kspace, headerLabelHeight) text:model.createDate font:17 textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(31, 31, 31, 1) backgroundColor:nil];
    [headerview addSubview:titleLabel];
    
    return headerview;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}



#pragma mark 网络请求
- (void)setRequest{
    [self.dynamicList removeAllObjects];
    self.dynamicList = nil;
    self.dynamicList = [NSMutableArray array];
    __block HouseTakeViewController *myself = self;
    currentPage= 1;
    [[LoadData object] loadDataPostAsynchronousWithURL:@"hkapp/house/getHouseDynamicByType" withDict:@{@"currentPage":[NSString stringWithFormat:@"%ld",(long)currentPage] ,@"rowCount":@"30",@"token":KTokenString,@"phone":KHkTellString,@"type":self.theType,@"houseSourceId":self.houseSourceId} superView:nil Block:^(id  _Nullable responseObject) {
        NSArray *list =KResponseData;
        for (int i = 0; i<list.count; i++) {
            HouseTakeModel *model = [[HouseTakeModel alloc]init];
            [model setValuesForKeysWithDictionary:list[i]];
            [myself.dynamicList addObject:model];
        }
        
        [myself.tableview reloadData];
        // 结束刷新
        [myself.tableview.mj_header endRefreshing];
        [myself.tableview.mj_footer resetNoMoreData];
    }];
}

- (void)setMoreRequest{
    __block HouseTakeViewController *myself = self;
    [[LoadData object] loadDataPostAsynchronousWithURL:@"hkapp/house/getHouseDynamicByType" withDict:@{@"currentPage":[NSString stringWithFormat:@"%ld",(long)currentPage] ,@"rowCount":@"30",@"token":KTokenString,@"phone":KHkTellString,@"type":self.theType,@"houseSourceId":self.houseSourceId} superView:nil Block:^(id  _Nullable responseObject) {
        NSArray *list =KResponseData;
        if (list.count >0) {
            for (int i = 0; i<list.count; i++) {
                HouseTakeModel *model = [[HouseTakeModel alloc]init];
                [model setValuesForKeysWithDictionary:list[i]];
                [myself.dynamicList addObject:model];
            }
            
            [myself.tableview reloadData];
            // 结束刷新
            [myself.tableview.mj_footer endRefreshing];
        }else{
            myself->currentPage--;
            [myself.tableview reloadData];
            [myself.tableview.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
